package com.drug.mingkang.service.impl;

import com.drug.mingkang.model.HqPurchasedetails;
import com.drug.mingkang.mapper.HqPurchasedetailsMapper;
import com.drug.mingkang.model.HqPurchaseorder;
import com.drug.mingkang.model.vo.PurchaseAddVo;
import com.drug.mingkang.model.vo.PurchasedetailsVo;
import com.drug.mingkang.model.vo.rawMaterialVo;
import com.drug.mingkang.service.HqPurchasedetailsService;
import com.drug.mingkang.service.HqPurchaseorderService;
import com.drug.mingkang.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 采购明细(HqPurchasedetails)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@Service("hqPurchasedetailsService")
public class HqPurchasedetailsServiceImpl implements HqPurchasedetailsService {
    @Resource
    private HqPurchasedetailsMapper hqPurchasedetailsMapper;
    @Resource
    private HqPurchaseorderService hqPurchaseorderService;

    @Override
    public List<Map<String, Object>> queryAllmaterial(String purchaseId) {
        return this.hqPurchasedetailsMapper.queryAllmaterial(purchaseId);
    }

    @Override
    public boolean deleteMaterials(String purchaseId) {
        return this.hqPurchasedetailsMapper.deleteMaterials(purchaseId);
    }

    @Override
    public boolean updateMaterial(String materialId, String purchaseId, int count) {
        boolean b = hqPurchasedetailsMapper.updateMaterial(materialId, purchaseId, count);
        List<PurchasedetailsVo> list=hqPurchasedetailsMapper.selectPurchaseSumprice(purchaseId);
        double purchaseSurplice = 0;
        for (PurchasedetailsVo purchasedetailsVo : list) {
            purchaseSurplice += purchasedetailsVo.getCount() * purchasedetailsVo.getMoney();
        }
        System.out.println(purchaseSurplice);
        hqPurchaseorderService.updatePurchaseSumprice(purchaseId,purchaseSurplice);
        return b;
    }

    @Override
    public boolean deleteMaterial(String materialId, String purchaseId) {
        return hqPurchasedetailsMapper.deleteMaterial(materialId,purchaseId);
    }

    @Override
    public boolean insertMaterial(String arr, String purchaseId) {
        List<rawMaterialVo> list = new Tools().jsonToList(arr, rawMaterialVo.class);
        HqPurchaseorder hqPurchaseorder=hqPurchaseorderService.selectRemarkOperator(purchaseId);
        int count=0;
        for (rawMaterialVo rawMaterialVo : list) {
            //设置订单详细
            HqPurchasedetails hqPurchasedetails=new HqPurchasedetails();
            hqPurchasedetails.setPurchaseId(purchaseId);
            hqPurchasedetails.setMaterialId(rawMaterialVo.getMaterialId());
            hqPurchasedetails.setMoney(rawMaterialVo.getMoney());
            hqPurchasedetails.setOperator(hqPurchaseorder.getOper());
            hqPurchasedetails.setRemark(hqPurchaseorder.getRemark());
            count=hqPurchasedetailsMapper.insert(hqPurchasedetails);
        }
        return count>0;
    }

    @Override
    public int deleteByPurchaseId(String purchaseId) {
        return this.hqPurchasedetailsMapper.deleteByPurchaseId(purchaseId);
    }

    @Override
    public int selectCount(PurchasedetailsVo purchasedetailsVo) {
        return this.hqPurchasedetailsMapper.selectCount(purchasedetailsVo);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param detailsId 主键
     * @return 实例对象
     */
    @Override
    public HqPurchasedetails queryById(String detailsId) {
        return this.hqPurchasedetailsMapper.queryById(detailsId);
    }

    /**
     * 查询多条数据
     *
     * @param purchasedetailsVo
     * @return 对象列表
     */
    @Override
    public List<HqPurchasedetails> queryAllByLimit(PurchasedetailsVo purchasedetailsVo) {
        return this.hqPurchasedetailsMapper.queryAllByLimit(purchasedetailsVo);
    }

    /**
     * 新增数据
     *
     * @param hqPurchasedetails 实例对象
     * @return 实例对象
     */
    @Override
    public HqPurchasedetails insert(HqPurchasedetails hqPurchasedetails) {
        this.hqPurchasedetailsMapper.insert(hqPurchasedetails);
        return hqPurchasedetails;
    }

    /**
     * 修改数据
     *
     * @param hqPurchasedetails 实例对象
     * @return 实例对象
     */
    @Override
    public HqPurchasedetails update(HqPurchasedetails hqPurchasedetails) {
        this.hqPurchasedetailsMapper.update(hqPurchasedetails);
        return this.queryById(hqPurchasedetails.getDetailsId());
    }

    /**
     * 通过主键删除数据
     *
     * @param detailsId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String detailsId) {
        return this.hqPurchasedetailsMapper.deleteById(detailsId) > 0;
    }
}